<?php
if (!empty($_FILES['pic'])){
  /* 设置允许上传文件的类型 */
    $type=array("jpg","jpeg","png","gif");
    /* 获取上传文件信息 */
    $upfile=$_FILES['pic'];
     $dest_dir=dirname(__FILE__)."/../upload/";
    /* 获取文件后缀名函数 */
    function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
    /* 判断上传文件类型 */
    if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) )
    {
        echo "<script>window.parent.upstate(0);alert('请上传jpg、png、gif格式的图片！');window.location.href='uploads.php';</script>";
    }
    else
    {   
          $newname = time().rand(10,99).".".strtolower( fileext($upfile['name']));
          $dest=$dest_dir.$newname;
          /* 移动上传文件到指定文件夹 */
          $state=move_uploaded_file($upfile['tmp_name'],$dest);
          if ($state)
          {  

              echo "<script>window.parent.upstate(0);window.parent.geturl('/upload/".$newname."');window.location.href='uploads.php';</script>";

          }
          else
          {
           echo "<script>window.parent.upstate(0);alert('图片上传失败，请重试！');window.location.href='uploads.php';</script>";
          }
     }
  exit();
}
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="text/javascript" src="/tpl/default/js/jquery-2.1.4.js"></script>
<body>
<form action="" method="post" id="frm1" enctype="multipart/form-data">
<input type="file" name="pic" id="pic" onchange="window.parent.upstate(1);$('#frm1').submit();">
</form>
<script>
function upfiles(){
  $("#pic").click();
}
</script>
</body>
</html>